<template>
<el-dialog :title="dialog.title" :visible.sync="dialog.visible" width="30%">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-position="top">
        <el-form-item label="父级渠道" prop="parent_id">
            <el-cascader v-model="ruleForm.parent_id" :options="tree" :props="{
            checkStrictly: true,
            label: 'title',
            value: 'id',
            emitPath: false,
          }" clearable></el-cascader>
        </el-form-item>
        <el-form-item label="渠道名称" prop="title">
            <el-input v-model="ruleForm.title" placeholder="请填写渠道名称"></el-input>
        </el-form-item>
        <el-form-item label="渠道代码" prop="code">
            <el-input v-model="ruleForm.code" placeholder="请填写渠道代码"></el-input>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
            <el-input v-model="ruleForm.sort"></el-input>
        </el-form-item>
        <el-form-item label="是否开启">
            <el-select v-model="ruleForm.status" placeholder="">
                <el-option label="开启" :value="1"></el-option>
                <el-option label="关闭" :value="0"></el-option>
            </el-select>
        </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
        <el-button @click="dialog.visible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
    </div>
</el-dialog>
</template>

<script>
import {
    getTreeApi,
    createSourceApi,
    saveSourceApi
} from "@/api/system/dictionary/source";
import {
    getFormToken
} from "@/api/system/system";

const ruleForm = {
    id: "",
    parent_id: "",
    title: "",
    code: "",
    sort: 0,
    status: 1,
    form_token: "",
};

export default {
    data() {
        return {
            dialog: {
                title: "",
                visible: false,
                type: "",
            },
            tree:[],
            ruleForm: Object.assign({}, ruleForm),
            rules: {
                title: [{
                        required: true,
                        message: "渠道名称必填",
                        trigger: "blur",
                    },
                    {
                        min: 1,
                        max: 30,
                        message: "长度在 1 到 30 个字符",
                        trigger: "blur",
                    },
                ],
                code: [{
                        required: true,
                        message: "渠道代码必填",
                        trigger: "blur",
                    },
                    {
                        min: 2,
                        max: 30,
                        message: "长度在 2 到 30 个字符",
                        trigger: "blur",
                    },
                ],
            },
        };
    },
    methods: {
        getTree() {
            getTreeApi({}).then((res) => {
                if (res.code == 200) {
                    this.tree = res.data.tree;
                } else {
                    this.$message.warning(res.msg);
                }
            });
        },
        showAddView() {
            let dialog = {
                title: "添加渠道",
                visible: true,
                type: "create",
            };
            this.dialog = dialog;
            this.getTree();
            // 初始化表单
            this.ruleForm = Object.assign({}, ruleForm);
            // 获取表单的token
            getFormToken({
                api: "/api/system.source/create",
            }).then(res => {
                this.ruleForm.form_token = res.data.token;
            });
        },
        showSaveView(row) {
            this.getTree();
            let dialog = {
                title: "编辑渠道",
                visible: true,
                type: "save",
            };
            this.dialog = dialog;
            // 初始化表单
            this.ruleForm = Object.assign({}, row);
            // 获取表单的token
            getFormToken({
                api: "/api/system.source/save",
            }).then(res => {
                this.ruleForm.form_token = res.data.token;
            });
        },
        submit() {
            this.$refs["ruleForm"].validate((valid) => {
                // 验证失败阻止提交
                if (!valid) return false;
                // 提交表单
                if (this.dialog.type == "create") {
                    createSourceApi(this.ruleForm)
                        .then((res) => {
                            if (res.code == "200") {
                                this.dialog.visible = false;
                                this.$emit("getTableData", {});
                                this.$message.success(res.msg);
                            } else {
                                this.$message.error(res.msg);
                            }
                        })
                        .catch((res) => {
                            this.$message.error(res.msg);
                        });
                } else {

                    saveSourceApi(this.ruleForm)
                        .then((res) => {
                            if (res.code == "200") {
                                this.dialog.visible = false;
                                this.$emit("getTableData", {});
                                this.$message.success(res.msg);
                            } else {
                                this.$message.error(res.msg);
                            }
                        })
                        .catch((res) => {
                            this.$message.error(res.msg);
                        });
                }
            });
        },
    },
};
</script>
